package com.zzz.algorithm.ysjz;

import sun.rmi.transport.tcp.TCPTransport;

public class Ysjz {

    private Integer[][] data;
    private int col;
    private int row;

    public Ysjz(int col, int row) {
        this.col = col;
        this.row = row;
        data = new Integer[col][row];
        for (int i = 0; i < data.length; i++) {
            Integer[] datum = data[i];
            for (int j = 0; j < datum.length; j++) {
                datum[j] = Integer.MAX_VALUE;
            }
        }
    }

    public void display() {
        for (int i = 0; i < data.length; i++) {
            Integer[] datum = data[i];
            for (int j = 0; j < datum.length; j++) {
                Integer i1 = datum[j];
                if (i1 == Integer.MAX_VALUE) {
                    System.out.print("*\t");
                } else {
                    System.out.print(i1 + "\t");
                }
            }
            System.out.println();
        }
    }

    public boolean del(int col, int row) {
        if (col < 0 || col >= this.col || row < 0 || row >= this.row) {
            throw new IllegalArgumentException("col or row  over range ");
        }
        return set0(col, row, Integer.MAX_VALUE);
    }

    public boolean set(int col, int row, int val) {
        if (val == Integer.MAX_VALUE) {
            throw new IllegalArgumentException("val is too big");
        }
        if (col < 0 || col >= this.col || row < 0 || row >= this.row) {
            throw new IllegalArgumentException("col or row  over range ");
        }
        return set0(col, row, val);
    }

    private boolean set0(int col, int row, int val) {

        data[col][row] = val;
        int c;
        int r;
        while (col < this.col && row < this.row) {
            c = col;
            r = row;
            if (c < this.col - 1 && big(val, data[c + 1][r])) {
                col = c + 1;
            }
            //如果存在下值 要判断右值是不是比上值小 (c<this.col-1 &&  big(data[c + 1][r], data[c][r + 1]))
            if (r < this.row - 1 &&(c<this.col-1 &&  big(data[c + 1][r], data[c][r + 1])) && big(val, data[c][r + 1])) {
                row = r + 1;
                col = c;
            }
            if (col == c && row == r) {
                break;
            }
            data[c][r] = data[col][row];
            data[col][row] = val;
        }

        return true;
    }


    public boolean insert(int val) {
        int col = this.col - 1;
        int row = this.row - 1;
        if (data[col][row] != Integer.MAX_VALUE) {
            return false;
        }
        data[col][row] = val;
        while (col >= 0 && row >= 0) {
            //上左有值 选择最大的一个交换
            if (col > 0 && row > 0) {
                //左大于上 并且大于插入的值 交换
                if (big(data[col][row - 1], val) && big(data[col][row - 1], data[col - 1][row])) {
                    data[col][row] = data[col][row - 1];
                    data[col][row - 1] = val;
                    row--;
                    // 上大于插入的值交换
                } else if (big(data[col - 1][row], val)) {
                    data[col][row] = data[col - 1][row];
                    data[col - 1][row] = val;
                    col--;
                } else {
                    break;
                }
                //上有值
            } else if (col > 0 && big(data[col - 1][row], val)) {
                data[col][row] = data[col - 1][row];
                data[col - 1][row] = val;
                col--;
                //左有值
            } else if (row > 0 && big(data[col][row - 1], val)) {
                data[col][row] = data[col][row - 1];
                data[col][row - 1] = val;
                row--;
            } else {
                break;
            }

        }
        if (data[col][row] != val) {
            return false;
        }
        return true;
    }

    public boolean contain(int val) {
        int col = 0;
        int row = this.row - 1;

        while (row >= 0 && col < this.col) {
            if (data[col][row] == val) {
                return true;
            } else if (big(val, data[col][row])) {
                col++;
            } else {
                row--;
            }
        }
        return false;
    }

    public boolean big(Integer i, Integer b) {
        return i > b;
    }


    public static void main(String[] args) {
        Ysjz ysjz = new Ysjz(2, 4);
        ysjz.insert(5);
        ysjz.insert(65);
        ysjz.insert(87);
        ysjz.insert(7);
        ysjz.insert(7);
        ysjz.insert(7);
        ysjz.insert(3);
        ysjz.display();
        ysjz.set(0,2,80);
        ysjz.display();
        ysjz.del(0,2);
        ysjz.display();
        System.out.println(ysjz.contain(7));
    }
}
